﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SGRapidForm.Common;
using SGRapidForm.Permission.Entity;
using FishWebLib;
using System.Collections.Specialized;
using System.Data;

namespace SGRapidForm.Permission.Component
{
    public class DeptComponent : ComponentBase
    {
        public bool Dept_Add(Dept dept)
        {
            return (FishBLLHelper.CallSpExecuteNonQuery("sgrf_permission_Dept_Add", dept) > 0);
        }

        //查询所有的部门
        public List<Dept> Dept_GetDeptAll()
        {
            return FishBLLHelper.CallSpGetDataItemList<Dept>("sgrf_permission_Dept_GetAll", null);
        }

        public bool Dept_Delete(string keyid)
        {
            return (FishBLLHelper.CallSpExecuteNonQuery("sgrf_permission_Dept_Delete", null, SqlHelper.GenerateSqlParameter(keyid, System.Data.DbType.String)) > 0);
        }

        public bool Dept_BulkDelete(string keyids)
        {
            string[] keyidCollection = keyids.Split('_');

            for (int i = 0; i < keyidCollection.Length; i++)
            {
                FishBLLHelper.CallSpExecuteNonQuery("sgrf_permission_Dept_Delete", null, SqlHelper.GenerateSqlParameter(keyidCollection[i], System.Data.DbType.String));
            }

            return true;
        }

        public bool Dept_Update(Dept dept)
        {
            return (FishBLLHelper.CallSpExecuteNonQuery("sgrf_permission_Dept_Update", dept) > 0);
        }

        public List<Dept> Dept_FilteredGetAll(string filterConditions)
        {
            NameValueCollection fieldCollection = SqlHelper.ParseQueryString(filterConditions);

            StringBuilder sql = new StringBuilder();
            sql.Append(SqlHelper.GenerateSqlStatement(fieldCollection, "DeptName", DbType.String, " AND DeptName like '%{0}%' "));
            sql.Append(SqlHelper.GenerateSqlStatement(fieldCollection, "DeptCode", DbType.String, " AND DeptCode like '{0}%' "));
            sql.Append(SqlHelper.GenerateSqlStatement(fieldCollection, "HelpCode", DbType.String, " AND HelpCode like '%{0}%' "));
            sql.Append(SqlHelper.GenerateSqlStatement(fieldCollection, "DeptType", DbType.Int32, " AND DeptType = '{0}' "));
            filterConditions = sql.ToString();
            return FishBLLHelper.CallSpGetDataItemList<Dept>("sgrf_permission_Dept_FilteredGetAll", null, filterConditions);
        }

        //通知通告对象选择的部门
        public List<Dept> Notice_FilteredGetAll(string filterConditions)
        {
            NameValueCollection fieldCollection = SqlHelper.ParseQueryString(filterConditions);

            StringBuilder sql = new StringBuilder();
            sql.AppendFormat("AND DeptCode LIKE '{0}%'",ApplicationContext.Current.DeptCode);
            sql.Append(SqlHelper.GenerateSqlStatement(fieldCollection, "DeptName", DbType.String, " AND DeptName like '%{0}%' "));
            sql.Append(SqlHelper.GenerateSqlStatement(fieldCollection, "DeptCode", DbType.String, " AND DeptCode like '{0}%' "));
            sql.Append(SqlHelper.GenerateSqlStatement(fieldCollection, "HelpCode", DbType.String, " AND HelpCode like '%{0}%' "));
            sql.Append(SqlHelper.GenerateSqlStatement(fieldCollection, "DeptType", DbType.Int32, " AND DeptType = '{0}' "));
            filterConditions = sql.ToString();
            return FishBLLHelper.CallSpGetDataItemList<Dept>("sgrf_permission_Dept_FilteredGetAll", null, filterConditions);
        }

        public Dept Dept_GetByKeyid(string keyid)
        {
            return FishBLLHelper.CallSpGetDataItem<Dept>("sgrf_permission_Dept_GetByKeyid", null, keyid);
        }

        /// <summary>
        /// 查询指定用户的被分配的部门
        /// </summary>
        public List<Dept> Dept_GetByUserKeyid(string userKeyid)
        {
            return FishBLLHelper.CallSpGetDataItemList<Dept>("sgrf_permission_Dept_GetByUserKeyid", null, userKeyid);
        }

        /// <summary>
        /// 查询指定用户未被分配的部门
        /// </summary>
        public List<Dept> Dept_GetUnmappedByUserKeyid(string userKeyid)
        {
            return FishBLLHelper.CallSpGetDataItemList<Dept>("sgrf_permission_Dept_GetUnmappedByUserKeyid", null, userKeyid);
        }

        ///// <summary>
        ///// 把部门分配给用户
        ///// </summary>
        ///// <param name="userKeyid">用户Keyid</param>
        ///// <param name="userName">用户名</param>
        ///// <param name="deptKeyid">部门Keyid</param>
        ///// <param name="deptCode">部门编码</param>
        ///// <returns></returns>
        //public bool Dept_AddUserAndDept(string userKeyid, string userName, string deptKeyid, string deptCode)
        //{
        //    return (FishBLLHelper.CallSpExecuteNonQuery("sgrf_permission_Dept_Delete", null, userKeyid,userName,deptKeyid,deptCode)>0);
        //}
    }
}
